package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@Api(tags = "套餐相关接口")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 通过id查询套餐
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        log.info("id: {}", id);
        SetmealVO setmealVO = setmealService.getSetmealAndDishById(id);
        return Result.success(setmealVO);
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> getSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("setmealPageQueryDTO: {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.getSetmealByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "新增套餐")
    @PostMapping()
    @CacheEvict(cacheNames = "setmealCache" , key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealVO: {}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "修改套餐")
    @PutMapping()
    @CacheEvict(cacheNames = "setmealCache" , key = "#setmealDTO.categoryId")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealVO: {}", setmealDTO);
        setmealService.updateSetmealAndDish(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除套餐")
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("ids: {}", ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    @ApiOperation(value = "修改套餐状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status, Long id) {
        log.info("status: {}, id: {}", status, id);
        SetmealDTO setmealDTO = SetmealDTO.builder()
                .id(id)
                .status(status)
                .build();
        setmealService.updateSetmealAndDish(setmealDTO);
        return Result.success();
    }

}
